package com.example.myapplication.shortvideo.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

/**
 * @author: rongfeng
 * @date: 2020/6/19
 * @description
 */
public class ShotVideoNoteAnimeView {
    private ImageView imgNote1, imgNote2;
    private ImageView imgVoiceCard;
    private ObjectAnimator rotation;
    private AnimatorSet mAnimatorSet1;
    private AnimatorSet mAnimatorSet2;
    private int mTx[], mTy[];

    public ShotVideoNoteAnimeView(ImageView imgNote1, ImageView imgNote2, ImageView imgVoiceCard) {
        this.imgNote1 = imgNote1;
        this.imgNote2 = imgNote2;
        this.imgVoiceCard = imgVoiceCard;
        mTx = new int[]{0,
                toPx(imgNote1.getContext(), -20),
                toPx(imgNote1.getContext(), -30),
                toPx(imgNote1.getContext(), -35),
                toPx(imgNote1.getContext(), -30),
                toPx(imgNote1.getContext(), -20),
                toPx(imgNote1.getContext(), -10)
        };
        mTy = new int[]{0,
                toPx(imgNote1.getContext(), -10),
                toPx(imgNote1.getContext(), -20),
                toPx(imgNote1.getContext(), -30),
                toPx(imgNote1.getContext(), -40),
                toPx(imgNote1.getContext(), -50),
                toPx(imgNote1.getContext(), -60)
        };
    }


    public static int toPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        int pxValue = (int) (dpValue * scale + 0.5f);
        return pxValue;
    }


    public void startAnime() {
        animeCard();
        mAnimatorSet1 = animeNote(mAnimatorSet1, imgNote1);
        mHandler.sendEmptyMessageDelayed(1, 1000);
    }

    public void stopAnime() {
        if (rotation != null) {
            rotation.cancel();
        }
        if (mAnimatorSet1 != null) {
            mAnimatorSet1.cancel();
        }
        if (mAnimatorSet2 != null) {
            mAnimatorSet2.cancel();
        }
        if (imgNote1 != null) {
            imgNote1.setVisibility(View.GONE);
        }
        if (imgNote2 != null) {
            imgNote2.setVisibility(View.GONE);
        }
        mHandler.removeMessages(1);
    }


    private AnimatorSet animeNote(AnimatorSet animatorSet, ImageView imgNote) {
        if (animatorSet == null) {

            ObjectAnimator alpha = ObjectAnimator.ofFloat(imgNote, "alpha", 0f, 1f, 0f).setDuration(2000);
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(imgNote, "translationX", mTx[0], mTx[1], mTx[2], mTx[3], mTx[4], mTx[5], mTx[6]).setDuration(2000);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(imgNote, "translationY", mTy[0], mTy[1], mTy[2], mTy[3], mTy[4], mTy[5], mTy[6]).setDuration(2000);
            ObjectAnimator rotate = ObjectAnimator.ofFloat(imgNote, "rotation", 0f, 45f).setDuration(2000);
            alpha.setRepeatCount(ValueAnimator.INFINITE);
            scaleX.setRepeatCount(ValueAnimator.INFINITE);
            scaleY.setRepeatCount(ValueAnimator.INFINITE);
            rotate.setRepeatCount(ValueAnimator.INFINITE);
            animatorSet = new AnimatorSet();
            animatorSet.playTogether(alpha, scaleX, scaleY, rotate);
            animatorSet.setDuration(2000);
            animatorSet.setInterpolator(new LinearInterpolator());


//            scaleX.addListener(new Animator.AnimatorListener() {
//                @Override
//                public void onAnimationStart(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationEnd(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationCancel(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationRepeat(Animator animator) {
//                    ObjectAnimator objectAnimator = (ObjectAnimator) animator;
//                    int rd = Math.random() > 0.5 ? 1 : 0;
//                    int tx = mTx[4];
//                    if (rd == 1) {
//                        tx = mTx[5];
//                    }
//                    objectAnimator.setFloatValues(mTx[0], mTx[1], mTx[2], mTx[3], tx);
//                }
//            });
//            rotate.addListener(new Animator.AnimatorListener() {
//                @Override
//                public void onAnimationStart(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationEnd(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationCancel(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationRepeat(Animator animator) {
//                    ObjectAnimator objectAnimator = (ObjectAnimator) animator;
//                    int rd = Math.random() > 0.5 ? 1 : 0;
//                    int r1 = 45;
//                    if (rd == 1) {
//                          r1 = - 45;
//                    }
//                    objectAnimator.setFloatValues(0f, r1);
//                }
//            });
        }
        imgNote.setVisibility(View.VISIBLE);
        animatorSet.start();
        return animatorSet;
    }

    private void animeCard() {
        if (rotation == null) {
            rotation = ObjectAnimator.ofFloat(imgVoiceCard, "rotation", 0f, 360f);
            rotation.setRepeatCount(ValueAnimator.INFINITE);
            rotation.setDuration(8000);
            rotation.setInterpolator(new LinearInterpolator());
        }
        rotation.start();
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                mAnimatorSet2 = animeNote(mAnimatorSet2, imgNote2);
            }
        }
    };


}
